package references

import (
	"gvm/instructions/base"
	"gvm/rtda"
	"gvm/rtda/heap"
)

type INSTANCE_OF struct {
	base.Index16Instruction
}

func (io *INSTANCE_OF) Execute(jFrame *rtda.JFrame) {
	stack := jFrame.OperandStack()
	ref := stack.PopRef()
	if ref == nil {
		stack.PushInt(0)
		return
	}
	cp := jFrame.Method().Class().ConstantPool()
	classRef := cp.GetConstant(io.Index).(*heap.ClassRef)
	class := classRef.ResolveClass()
	if ref.IsInstanceOf(class) {
		stack.PushInt(1)
	} else {
		stack.PushInt(0)
	}
}
